package cn.leizp.springboot.auth.frontweb.service;

import cn.leizp.springboot.auth.frontweb.entity.User;
import cn.leizp.springboot.auth.frontweb.repository.UserRepository;
import org.springframework.stereotype.Service;

import java.util.List;

@Service
public class UserService {
    private final UserRepository userRepository;

    public UserService(UserRepository userRepository) {
        this.userRepository = userRepository;
    }

    public User save(User user) {
        return userRepository.save(user);
    }

    public User findById(Integer id) {
        return userRepository.findUserById(id);
    }

    public List<User> findAll() {
        return userRepository.findAll();
    }

    public void deleteById(Integer id) {
        delete(findById(id));
    }

    public void delete(User user) {
        userRepository.delete(user);
    }

    public User getByUsername(String username) {
        List<User> userList = userRepository.findUsersByName(username);
        if (userList.size() > 0) {
            return userList.get(0);
        }
        return null;
    }
}
